$(function () {
    //抽取对象
    var stockOutcomeBill_dialog = $("#stockOutcomeBill_dialog");
    var stockOutcomeBill_datagrid = $("#stockOutcomeBill_datagrid");
    var stockOutcomeBill_form = $("#stockOutcomeBill_form");

    stockOutcomeBill_datagrid.datagrid({
        fitColumns: true,
        url:'/stockOutcomeBill/query.do',
        toolbar:'#stockOutcomeBill_toolbar',
        striped: true,
        rownumbers: true,
        singleSelect: true,
        ctrlSelect: true,
        pagination: true,
        pageNumber: 10,
        scrollbarSize: 0,
        fit: true,
        columns: [[
            {field: 'sn', title: '订单编码', width: 100},
            {
                field: 'supplier', title: '供应商', width: 100, formatter: function (value, row, index) {
                    return row ? row.s_name : "";
                }
            },
            {field: 'totalNumber', title: '数量', width: 100},
            {field: 'totalAmount', title: '合计', width: 100},
            {
                field: 'status', title: '退货状态', width: 100, formatter: function (value, row, index) {
                    return value ==2 ? "<font color='green'>已退货</font>" : "<font color='red'>正在退货</font>";
                }
            },
            {
                field: 'depot', title: '退货仓库', width: 100, formatter: function (value, row, index) {
                    return row ? row.d_name : "";
                }
            },
            {field: 'inputTime', title: '退货日期', width: 100}
        ]]
    })

    stockOutcomeBill_dialog.dialog({
        width: 900,
        height: 500,
        buttons: '#refund_btns',
        closed: true

    })

    $("#product_view").dialog({
        width: 700,
        height: 420,
        closed: true
    })

//同一管理执行的方法
    var methodObj = {
        add: function () {
            //打开弹出框
            stockOutcomeBill_dialog.dialog('open');
            //设置弹出框的标题
            stockOutcomeBill_dialog.dialog('setTitle', '新增退货');
        },
        //打开关联第二个datagrit
        addProduct: function () {
            $("#product_view").dialog('open')
        },

        //关闭页面
        cancel: function () {
            stockOutcomeBill_dialog.dialog('close')
        },
        //表单刷新按钮
        reload: function () {
            stockOutcomeBill_datagrid.datagrid("reload");
        },
        //删除明细
        remove:function () {
            var currentTr = $(this).closest("tr");
            currentTr.remove();
        },
        changeAudit: function () {
            //判断是否有选中数据
            var row = stockOutcomeBill_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                return;
            }
            //弹出确认框
            $.messager.confirm('温馨提示', '您想要执行该操作吗?', function (y) {
                if (y) {
                    $.get("/stockOutcomeBill/audit.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', data.msg, 'info', function () {
                                stockOutcomeBill_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    }, "json")
                }
            });
        },
        save: function () {
            $.each($("#tr_appendTo tr"), function (index, item) {
                $(item).find("[tag=pid]").prop("name", "items[" + index + "].product.id");
                $(item).find("[tag=salePrice]").prop("name", "items[" + index + "].salePrice");
                $(item).find("[tag=number]").prop("name", "items[" + index + "].number");
                $(item).find("[tag=remark]").prop("name", "items[" + index + "].remark");
            });
            stockOutcomeBill_form.form("submit", {
                url: '/stockOutcomeBill/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            //重新加载数据
                            stockOutcomeBill_datagrid.datagrid("reload");
                            //关闭弹窗
                            stockOutcomeBill_dialog.dialog("close");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            });
        }
    }



    //统一绑定事件
    $("a[data-cmd]").click(function () {
        //获取了当前点击的方法
        var method = $(this).data("cmd");
        //调用同一执行的方法,传入执行的属性
        methodObj[method]();
    });

    //显示商品的datagrid
    $("#product_datagrid").datagrid({
        fitColumns: true,
        url: '/product/query.do',
        striped: true,
        rownumbers: true,
        singleSelect: true,
        ctrlSelect: true,
        pagination: true,
        pageNumber: 10,
        scrollbarSize: 0,
        fit: true,

        columns: [[
            {field: 'sn', title: '商品编码', width: 100},
            {field: 'name', title: '商品名称', width: 100},
            {field: 'salePrice', title: '单价', width: 100},
            {field: 'remark', title: '备注', width: 100}
        ]],
        //双击添加商品
        onDblClickRow:function (index,row) {
            var cl = $("#itemTr tr:first").clone();
            cl.appendTo("#tr_appendTo");
            cl.find("span[tag=sn]").html(row.sn);
            cl.find("input[tag=pid]").val(row.id);
            cl.find("[tag=productName]").html(row.name);
            cl.find("[tag=salePrice]").val(row.salePrice);
            $("#product_view").dialog('close');
        }

    });
    $("#tr_appendTo").on("change","[tag=number],[tag=salePrice]",function () {
        var currentTr = $(this).closest('tr');
        var price = currentTr.find("[tag=salePrice]").val() || 0;
        var number = currentTr.find("[tag=number]").val() || 0;
        currentTr.find("[tag=countPrice]").html((price * number).toFixed(2));
    }).on("click",".del",function () {
        var tr = $(this).closest("tr");
        tr.remove();
    })
})
